﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Koonsoft.CustomerService.Entity;
using Koonsoft.CustomerService.BLL;

namespace CustomerService.ReportIssues
{
    public partial class SearchIssues : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Bind(1);
        }

        protected void AspNetPager1_PageChanged(object src, EventArgs e)
        {
            Bind(AspNetPager1.CurrentPageIndex);
        }

        private void Bind(int pageindex)
        {
            labError.Text = "";
            AspNetPager1.Visible = true;

            Issue issue = new Issue();
            issue.IssueTitle = txtIssueTitle.Text;
            issue.IssueLevel = dropIssueLevel.SelectedValue == "-1" ? "" : dropIssueLevel.SelectedValue;
            issue.IssueCategory = dropIssueCategory.SelectedValue == "-1" ? "" : dropIssueCategory.SelectedValue;
            issue.IssueSource = dropIssueSource.SelectedValue == "-1" ? "" : dropIssueSource.SelectedValue;
            issue.ClientVersion = dropClientVersion.SelectedValue == "-1" ? "" : dropClientVersion.SelectedValue;
            issue.CustomersQQ = txtCustomersQQ.Text.Trim();
            issue.CustomersMobile = txtCustomersMobile.Text.Trim();
            issue.ServiceArea = dropServiceArea.SelectedValue == "-1" ? "" : dropServiceArea.SelectedValue;
            issue.Region = txtRegion.Text.Trim();
            issue.BandWidth = dropBandWidth.SelectedValue == "-1" ? "" : dropBandWidth.SelectedValue;
            issue.SystemEnvironment = dropSystemEnvironment.SelectedValue == "-1" ? "" : dropSystemEnvironment.SelectedValue;
            issue.DecodingRate = txtDecodingRate.Text.Trim();
            issue.CreateUser = txtCreateUser.Text.Trim();
            issue.Status = Convert.ToInt32(dropState.SelectedValue);

            issue.ID = txtId.Text.Trim();
            issue.GSID = txtGSID.Text.Trim();
            issue.UserName = txtUserName.Text.Trim();
            issue.UserAccounts = txtUserAccounts.Text.Trim();
            issue.PrincipalID = dropPrincipal.SelectedValue == "-1" ? "" : dropPrincipal.SelectedValue;
            issue.OperatorsId = dropOperators.SelectedValue == "-1" ? "" : dropOperators.SelectedValue;

            DateTime dtStart = new DateTime();
            DateTime dtEnd = new DateTime();
            try
            {
                dtStart = DateTime.Parse(txtStartTime.Text);
            }
            catch
            {
            }
            try
            {
                dtEnd = DateTime.Parse(txtEndTime.Text);
                if (dtEnd < dtStart)
                {
                    labError.Text = "结束时间应大于等于开始时间";
                    return;
                }
            }
            catch
            {
            }

            string msg;
            List<Issue> issues = new IssuesBLL().GetIssues(issue, out msg, dtStart, dtEnd);

            AspNetPager1.RecordCount = issues.Count;
            PagedDataSource pds = new PagedDataSource();
            pds.DataSource = issues;
            pds.AllowPaging = true;
            pds.CurrentPageIndex = pageindex - 1;
            try
            {
                pds.PageSize = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["PageSize"]);
                AspNetPager1.PageSize = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["PageSize"]);
            }
            catch
            {
                pds.PageSize = 15;
                AspNetPager1.PageSize = 15;
            }
            GridView1.DataSource = pds;
            GridView1.DataBind();
        }
    }
}